package com.showtao.front.controller;

import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.showtao.common.controller.BaseController;
import com.showtao.common.dto.ResultDto;
import com.showtao.common.dto.ResultDtoFactory;
import com.showtao.front.domain.MobileUser;
import com.showtao.front.domain.ProductCategory;
import com.showtao.front.service.MobileUserService;
import com.showtao.front.service.ProductService;
import com.showtao.front.service.VehicleService;

@Controller
@RequestMapping(value = "/mobile")
public class MobileIndexController extends BaseController {
	@Autowired
    private MobileUserService mobileUserService;
	@Autowired
	private ProductService productService;
	@Autowired
	private VehicleService vehicleService;

	@RequestMapping(value = "/")
	public String index(Model model) throws Exception {
		return "mobile_home";
	}
	
	@RequestMapping(value = "/home")
	public String home(Model model) throws Exception {
		return "mobile_home";
	}

	@RequestMapping(value = "/about")
	public String about(Model model) throws Exception {
		return "mobile_about";
	}
	
	@RequestMapping(value = "/selection")
	public String selectionCategory(Model model) throws Exception {
		List<ProductCategory> productCategories = productService.findAllProductCategories();
		model.addAttribute("categories",productCategories);
		return "mobile_selection_category";
	}

	@RequestMapping(value = "/selection/detail")
	public String selection(Model model, String categoryCode, String categoryName) throws Exception {
		List<ProductCategory> productCategories = productService.findAllProductCategories();
		model.addAttribute("categories",productCategories);
		ProductCategory defaultCategory = new ProductCategory();
		defaultCategory.setCode(categoryCode);
		defaultCategory.setName( URLDecoder.decode(categoryName,"UTF-8"));
		model.addAttribute("defaultCategory",defaultCategory);
		model.addAttribute("vehiclesMap", vehicleService.findAllVehicleBrandMap());
		return "mobile_selection";
	}

	@RequestMapping(value = "/loginPage")
	public String loginPage(Model model) throws Exception {
		return "mobile_login";
	}
	
	@RequestMapping(value = "/logOut")
	public String logOut(Model model, HttpSession session) throws Exception {
		session.removeAttribute("mobileUser");
		return "redirect:selection";
	}

	@RequestMapping(value = "/register")
	public String registerPage(Model model) throws Exception {
		return "mobile_register";
	}

	@RequestMapping(value = "/modifyPwdPage")
	public String modifyPwdPage(Model model) throws Exception {
		return "mobile_modify_pwd";
	}

	@RequestMapping(value = "/findPassword")
	public String findPasswordPage(Model model) throws Exception {
		return "mobile_find_pwd";
	}
	
	@RequestMapping(value = "/modifyPwd", method = RequestMethod.POST)
	@ResponseBody
	public ResultDto modifyPwd(Model model, String oldPwd, String newPwd, HttpSession session)
			throws Exception {
		
		MobileUser mu=(MobileUser)session.getAttribute("mobileUser");
		if(mu==null){
			return ResultDtoFactory.toAck("errorMsg", "用户未登录");
		}
		String password = mobileUserService.getByMobile(mu.getMobile()).getPassword();
		model.addAttribute("mobile", mu.getMobile());
		if(!password.equals(oldPwd)){
			return ResultDtoFactory.toAck("errorMsg", "原密码不正确");
		}
		mu.setPassword(newPwd);
		mobileUserService.saveMobileUser(mu);
		return ResultDtoFactory.toAck("successMsg","修改成功");
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public ResultDto login(Model model, String mobile, String pwd, HttpSession session)
			throws Exception {
		MobileUser mu=mobileUserService.getByMobile(mobile);
		if(mu != null){
			if(mu.getPassword() != null && mu.getPassword().equals(pwd)){
				session.setAttribute("mobileUser", mu);
			}else{
				return ResultDtoFactory.toAck("errorMsg", "密码错误，请输入正确的密码！");
			}
		}else{
			return ResultDtoFactory.toAck("errorMsg", "此账户不存在，请注册后再登录！");
		}
		return ResultDtoFactory.toAck("successMsg","登录成功");
	}

	@RequestMapping(value = "/findPassword/send_sms", method = RequestMethod.POST)
	@ResponseBody
	public ResultDto doSendSMS(String mobile) throws Exception {
		if(mobileUserService.getByMobile(mobile) == null){
			return ResultDtoFactory.toAck("errorMsg", "此手机号未在本网站注册！");
		}
		try{
			mobileUserService.sendValidationCode(mobile);
    	}catch(Exception ex){
    		return ResultDtoFactory.toAck("errorMsg",ex.getMessage());
    	}
		return ResultDtoFactory.toAck("短信验证码发送成功");
	}
	
	@RequestMapping(value = "/findPassword/verify_code", method = RequestMethod.POST)
	@ResponseBody
	public ResultDto doVerifyCode(String mobile, String verifyCode) throws Exception {
		try{
			mobileUserService.validateCode(mobile, verifyCode);
		}catch(Exception ex){
			return ResultDtoFactory.toAck("errorMsg", ex.getMessage());
		}
		return ResultDtoFactory.toAck("验证成功");
	}
	
	@RequestMapping(value = "/findPassword/modify_Pwd", method = RequestMethod.POST)
	@ResponseBody
	public ResultDto modifyPwd(String mobile, String pwd, Model model) throws Exception {
		MobileUser mu=mobileUserService.getByMobile(mobile);
		if(mu != null){
			mu.setPassword(pwd);
			mobileUserService.saveMobileUser(mu);
		}else{
			return ResultDtoFactory.toAck("errorMsg", "此手机号未在本网站注册！");
		}
		return ResultDtoFactory.toAck("successMsg","修改成功");
	}
	
	@RequestMapping(value = "/register/send_sms", method = RequestMethod.POST)
	@ResponseBody
	public ResultDto doRegisterSendSMS(String mobile) throws Exception {
		if(mobileUserService.getByMobile(mobile) != null){
			return ResultDtoFactory.toAck("errorMsg", "此手机号已经被注册，不可重复注册！");
		}
		try{
			mobileUserService.sendValidationCode(mobile);
    	}catch(Exception ex){
    		return ResultDtoFactory.toAck("errorMsg",ex.getMessage());
    	}
		return ResultDtoFactory.toAck("短信验证码发送成功");
	}
	
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	@ResponseBody
	public ResultDto doRegister(Model model,String mobile, String pwd, String verifyCode) throws Exception {
		MobileUser mu = new MobileUser();
		mu.setMobile(mobile);
		mu.setPassword(pwd);
		mu.setCreatedAt(new Date());
		if(mobileUserService.getByMobile(mobile) != null){
			return ResultDtoFactory.toAck("errorMsg", "此手机号已经被注册，不可重复注册！");
		}
		try{
			mobileUserService.validateCode(mobile, verifyCode);
		}catch(Exception ex){
			return ResultDtoFactory.toAck("errorMsg", ex.getMessage());
		}
		mobileUserService.registerMobileUser(mu);
		return ResultDtoFactory.toAck("successMsg","注册成功");
	}

}
